from django.db import models
from django.shortcuts import reverse
from django.utils.translation import gettext_lazy as _
from .project import Project
from lib.django import models as mymodels

from .lib import ProjectSubMixin

class HumanGeneticResource(ProjectSubMixin, models.Model):
    project = models.OneToOneField(
        verbose_name=_("项目"),
        to=Project,
        on_delete=models.CASCADE,
    )

    is_team_leader = mymodels.YesNoField(
        verbose_name="是否是组长",
        blank=True,
        default=False
    )
    
    is_partner_unit = mymodels.YesNoField(
        verbose_name="是否是合作单位",
        blank=True,
        default=False
    )

    hgrac_online_acceptance_date = models.DateField(
        verbose_name="HGRAC申请在线接受日期", null=True, blank=True, default=None
    )
    hgrac_announcement_date = models.DateField(
        verbose_name="HGRAC公示日期", null=True, blank=True, default=None
    )
    hgrac_online_submission_return_count = models.IntegerField(
        verbose_name="HGRAC在线递交退回次数", null=True, blank=True, default=0
    )

    class Meta:
        default_permissions = []
        db_table = "project_human_genetic_resource"
        verbose_name = _("人类遗传资源申请")
        verbose_name_plural = verbose_name

    @classmethod
    def get_link(cls, project_id, id):
        return reverse("project:beginning/detail", kwargs={'project_pk': project_id})

class HGRSubModel(models.Model):
    """
    Abstract Model
    """
    HGR = models.ForeignKey(
        verbose_name=_("人类遗传资源"),
        to=HumanGeneticResource,
        on_delete=models.CASCADE,
    )
    
    approval_time = models.DateField(
        verbose_name="获批时间",
        null=True,
        blank=True,
        default=None
    )

    class Meta:
        abstract = True

    @classmethod
    def get_link(cls, project_id, id):
        return reverse("project:human_genetic_resource/detail", kwargs={'project_pk': project_id, 'pk': id})

class HGRInternationalCooperation(HGRSubModel):
    class Meta:
        default_permissions = []
        db_table = "project_hgr_international_cooperation"
        verbose_name = "人类遗传资源-国际合作批准"
        verbose_name_plural = verbose_name

class HGRCollection(HGRSubModel):
    class Meta:
        default_permissions = []
        db_table = "project_hgr_collection"
        verbose_name = "人类遗传资源-采集批准"
        verbose_name_plural = verbose_name

class HGRStorage(HGRSubModel):
    class Meta:
        default_permissions = []
        db_table = "project_hgr_storage"
        verbose_name = "人类遗传资源-保藏批准"
        verbose_name_plural = verbose_name

class HGRMaterialExport(HGRSubModel):
    class Meta:
        default_permissions = []
        db_table = "project_hgr_material_export"
        verbose_name = "人类遗传资源-材料出境批准"
        verbose_name_plural = verbose_name

class HGRDataFiling(HGRSubModel):
    class Meta:
        default_permissions = []
        db_table ="project_hgr_data_filing"
        verbose_name = "人类遗传资源-数据备案批准"
        verbose_name_plural = verbose_name
